package entities.profiles;

import entities.Vehicle;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "vehicle_profile")
@NamedQueries({
    @NamedQuery(name = "VehicleProfile.findAll", query = "SELECT v FROM VehicleProfile v"),
    @NamedQuery(name = "VehicleProfile.findByIdVehicleProfile", query = "SELECT v FROM VehicleProfile v WHERE v.idVehicleProfile = :idVehicleProfile"),
    @NamedQuery(name = "VehicleProfile.findByEvaluationsTotal", query = "SELECT v FROM VehicleProfile v WHERE v.evaluationsTotal = :evaluationsTotal"),
    @NamedQuery(name = "VehicleProfile.findByCleanliness", query = "SELECT v FROM VehicleProfile v WHERE v.cleanliness = :cleanliness"),
    @NamedQuery(name = "VehicleProfile.findBySafety", query = "SELECT v FROM VehicleProfile v WHERE v.safety = :safety"),
    @NamedQuery(name = "VehicleProfile.findByConservation", query = "SELECT v FROM VehicleProfile v WHERE v.conservation = :conservation")})
public class VehicleProfile implements Serializable {

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "idVehicleProfile")
    private Vehicle vehicle;
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idVehicleProfile")
    private Integer idVehicleProfile;

    @Basic(optional = false)
    @NotNull
    @Column(name = "evaluationsTotal")
    private long evaluationsTotal;

    @Basic(optional = false)
    @NotNull
    @Column(name = "cleanliness")
    private long cleanliness;

    @Basic(optional = false)
    @NotNull
    @Column(name = "safety")
    private long safety;

    @Basic(optional = false)
    @NotNull
    @Column(name = "conservation")
    private long conservation;

    /*@OneToOne(cascade = CascadeType.ALL, mappedBy = "idVehicleProfile")
     private Vehicle vehicle;*/
    public VehicleProfile() {
        this.evaluationsTotal = 0;
        this.cleanliness = 0;
        this.safety = 0;
        this.conservation = 0;
    }

    /*public VehicleProfile(Vehicle vehicle) {
     this.vehicle = vehicle;
     this.evaluationsTotal = 0;
     this.cleanliness = 0;
     this.safety = 0;
     this.conservation = 0;
     }*/
    public Integer getId() {
        return idVehicleProfile;
    }

    public void evaluate(int cleanliness, int safety, int conservation) {
        if (cleanliness > 10) {
            cleanliness = 10;
        }
        if (safety > 10) {
            safety = 10;
        }
        if (conservation > 10) {
            conservation = 10;
        }
        if (cleanliness < 1) {
            cleanliness = 1;
        }
        if (safety < 1) {
            safety = 1;
        }
        if (conservation < 1) {
            conservation = 1;
        }
        ++evaluationsTotal;
        this.cleanliness += cleanliness;
        this.safety += safety;
        this.conservation += conservation;
    }

    public long getEvaluationsTotal() {
        return evaluationsTotal;
    }

    public long getCleanliness() {
        return cleanliness;
    }

    public long getSafety() {
        return safety;
    }

    public long getConservation() {
        return conservation;
    }

    public long getCleanlinessAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return cleanliness/evaluationsTotal;
    }

    public long getSafetyAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return safety/evaluationsTotal;
    }

    public long getConservationAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return conservation/evaluationsTotal;
    }

    /*public Vehicle getVehicle() {
     return vehicle;
     }*/
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idVehicleProfile != null ? idVehicleProfile.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (object == null) {
            return false;
        }
        if (!(object instanceof VehicleProfile)) {
            return false;
        }
        VehicleProfile other = (VehicleProfile) object;
        return !((this.idVehicleProfile == null && other.idVehicleProfile != null) || (this.idVehicleProfile != null && !this.idVehicleProfile.equals(other.idVehicleProfile)));
    }

    @Override
    public String toString() {
        return "entities.profiles.VehicleProfile[ idVehicleProfile=" + idVehicleProfile + " ]";
    }

    public Vehicle getVehicle() {
        return vehicle;
    }

    public void setVehicle(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

}
